<?php
/**
 * Author: Traveler
 * Time  : 2021/9/4 10:11
 */

namespace app\model;


use sunshine\basic\BaseModel;
use think\model\relation\BelongsToMany;

class RoleModel extends BaseModel
{
    /**
     * 模型名称
     *
     * @var string
     */
    protected $name = 'role';

    /**
     * 搜索器：id
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchIdAttr($query, $value, $data)
    {
        $query->where('id', $value);
    }

    /**
     * 搜索器：name
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchNameAttr($query, $value, $data)
    {
        $query->whereLike('name', '%' . $value . '%');
    }

    /**
     * 搜索器：status
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchStatusAttr($query, $value, $data)
    {
        $query->where('status', $value);
    }

    /**
     * 模型关联：角色__权限
     *
     * @return BelongsToMany
     */
    public function menus(): BelongsToMany
    {
        return $this->belongsToMany(MenuModel::class, 'role__menu', 'menu_id', 'role_id');
    }
}